package com.ss.android.f;

import android.accounts.Account;
import android.app.Activity;
import android.content.Context;
import android.os.Process;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;
import com.ss.android.common.AppContext;
import com.ss.android.common.applog.NetUtil;
import com.ss.android.f.a.RegistrationHeaderHelper;
import com.ss.android.f.a.c;
import com.ss.android.f.b.a.a.AccountCacheHelper;
import com.ss.android.f.b.RegisterServiceController;

import org.json.JSONObject;

import java.util.Map;

public class DeviceRegisterManager {
    public interface a$a {
        void a(String arg1, String arg2);

        void a(boolean arg1, boolean arg2);
    }

    private static volatile DeviceRegisterManager a;
    private static volatile boolean b;
    private static Context c;
    private static boolean d;
    private final RegisterServiceController e;

    static {
        DeviceRegisterManager.b = false;
    }

    private DeviceRegisterManager() {
        this.e = new RegisterServiceController(a.c);
        com.ss.android.f.b.aa.a(a.d);
        RegistrationHeaderHelper.a(this.e);
        RegisterServiceController v0 = this.e;
        v0.c = new JSONObject();
        if(!RegistrationHeaderHelper.a(v0.b, v0.c) && (Logger.debug())) {
            throw new RuntimeException("init header error.");
        }

        new RegisterServiceController.DeviceRegisterThread(v0).start();
    }

    public static void a(c arg0) {
        RegisterServiceController.a(arg0);
    }

    public static void a(boolean arg0) {
        a.d = arg0;
    }

    public static void a(Context context) throws IllegalArgumentException {
        if(context == null) {
            throw new IllegalArgumentException("context = null");
        }

        DeviceRegisterManager.b = true;
        if((context instanceof Activity)) {
            DeviceRegisterManager.d = true;
        }

        DeviceRegisterManager.c = context.getApplicationContext();
        if(a.a == null) {
            Class v1 = DeviceRegisterManager.class;
            synchronized (v1){
                if(DeviceRegisterManager.a == null) {
                    DeviceRegisterManager.a = new DeviceRegisterManager();
                }
            }
        }

        if(Logger.debug()) {
            Logger.d("DeviceRegisterManager", "DeviceRegister init, DeviceRegister : " + DeviceRegisterManager.a.toString() + ", process : " + Process.myPid());
        }
    }

    public static void a(Context arg2, Account arg3) {
        AccountCacheHelper v0 = com.ss.android.f.b.a.aa.a(arg2).a;
        if(arg3 != null) {
            v0.b = arg3;
            if(v0.c.size() > 0) {
                ThreadPlus.submitRunnable(new com.ss.android.f.b.a.a.b(v0, arg3));
            }
        }
    }

    public static void a(AppContext arg0) {
        RegistrationHeaderHelper.a(arg0);
        NetUtil.a(arg0);
    }

    public static void a(com.ss.android.f.a.b arg0) {
        RegisterServiceController.a(arg0);
    }

    public static String a() {
        DeviceRegisterManager v1 = DeviceRegisterManager.a;
        String v0 = "";
        if(v1 != null) {
            v0 = v1.e.a();
        }

        if(Logger.debug()) {
            Logger.d("DeviceRegisterManager", "getDeviceId() called,return value : " + v0);
        }

        return v0;
    }

    public static void a(Map arg4) {
        DeviceRegisterManager v0 = DeviceRegisterManager.a;
        if(arg4 != null && v0 != null) {
            DeviceRegisterManager v1 = DeviceRegisterManager.a;
            String v0_1 = "";
            if(v1 != null) {
                v0_1 = v1.e.a.a();
            }

            if(Logger.debug()) {
                Logger.d("DeviceRegisterManager", "getOpenUdId() called,return value : " + v0_1);
            }

            if(v0_1 != null) {
                arg4.put("openudid", v0_1);
            }

            v0_1 = a.getClientUDID();
            if(v0_1 != null) {
                arg4.put("clientudid", v0_1);
            }

            v1 = DeviceRegisterManager.a;
            v0_1 = "";
            if(v1 != null) {
                v0_1 = v1.e.d;
                if(Logger.debug()) {
                    Logger.d("DeviceRegisterManager", "getInstallId() called,return value : " + v0_1);
                }
            }

            if(v0_1 != null) {
                arg4.put("install_id", v0_1);
            }

            v0_1 = a.a();
            if(v0_1 == null) {
                return;
            }

            arg4.put("device_id", v0_1);
        }
    }

    public static void a(Context arg2, String arg3) {
        DeviceRegisterManager v0 = DeviceRegisterManager.a;
        if(a.a != null) {
            RegisterServiceController v0_1 = v0.e;
            if(!android.support.design.a.isTextEmpty(arg3)) {
                try {
                    v0_1.e = arg3;
                }
                catch(Exception v0_2) {
                    v0_2.printStackTrace();
                }
            }
            RegistrationHeaderHelper.b(arg2, arg3);
        }
    }

    public static void a(int arg0) {
        RegistrationHeaderHelper.a(arg0);
    }

    public static void a(String arg0) {
        com.ss.android.f.b.aa.a(arg0);
    }

    public static void a(a$a arg0) {
        RegisterServiceController.a(arg0);
    }

    public static String b(Context arg1) {
        return RegistrationHeaderHelper.a(arg1);
    }

    public static void b(String arg0) {
        RegistrationHeaderHelper.b(arg0);
    }

    public static String getClientUDID() {
        DeviceRegisterManager v1 = DeviceRegisterManager.a;
        String v0 = "";
        if(v1 != null) {
            v0 = v1.e.a.b();
        }

        if(Logger.debug()) {
            Logger.d("DeviceRegisterManager", "getClientUDID() called,return value : " + v0);
        }

        return v0;
    }

    public static void c() {
        RegisterServiceController.b();
    }

    public static void d() {
        RegisterServiceController.b();
    }

    public static void e() {
        RegisterServiceController.a(a.c);
    }
}

